package momo.app.structure;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class SessionController
{
	private static SessionController controller;
	private AtomicInteger sid;
	private Log log = LogFactory.getLog(SessionController.class);
	private Log logStructure = LogFactory.getLog("MoMoStructureLog");
	
	private SessionController()
	{
		sid = new AtomicInteger(0);
	}
	
	static public SessionController getInstance()
	{
		if ( controller == null)
			controller = new SessionController();
		return controller;
	}
	
	public void setSessionId(int sessionId)
	{
		sid.getAndSet(sessionId);
		if ( log.isDebugEnabled() )
			log.debug("SID: " + sessionId);
		if ( logStructure.isDebugEnabled() )
			logStructure.debug("SID: " + sessionId);
	}
	
	public int getSessionId()
	{
		return sid.get();
	}
	
	public boolean isSameSession(int sessionId)
	{
		return sid.get() == sessionId;
	}
	
	public void initSession()
	{
		this.setSessionId( this.generate() );
	}
	
	public int generate()
	{
		Random rand = new Random();
		return rand.nextInt();
	}
}//end of SessionController
